<script>
import { mapWritableState } from 'pinia'
import { User, Lock } from '@element-plus/icons-vue'
import { useAuthStore } from '@/stores/authStore.js'
import userRules from '@/rules/userRules.js'
import { login } from "@/api/authApi.js"
//import axios from 'axios'
export default {
  components: {
    User,
    Lock,
  },
  data() {
    return {
      user: {
        username: '',
        password: '',
        usertype: '1',
      },
      userRules: userRules,
      //userRules: {
      //username: [
      //{ required: true, message: '登录名不能为空', trigger: 'blur' },
      //{ min: 2, message: '登录名不能低于两位', trigger: 'blur' }
      //],
      //password: [
      //{ required: true, message: '密码不能为空', trigger: 'blur' }
      //],
      //},
    }
  },
  computed: {
    ...mapWritableState(useAuthStore, ['currentUser', 'menus', 'token']),
  },
  methods: {
    async doLogin() {
      this.currentUser = null
      this.token = null
      this.menus = []
      await this.$refs.LoginForm.validate()
      let data = await login(this.user);
      //console.log(result.data.data.user);
      //console.log(result.data.data.token);
      //this.currentUser = result.data.data.user;
      //this.token = result.data.data.token;
      //console.log(data);
      this.currentUser = data.user;
      this.token = data.token;

      this.$router.push({
        name: "home"
      })
    },
  },
}
</script>
<template>
  <div class="box">
    <!-- UI组件本身是带有样式的 -->
    <el-card>
      <template #header> 教务系统 </template>
      <el-form ref="LoginForm" :model="user" :rules="userRules">
        <el-form-item prop="username">
          <el-input placeholder="学号/工号/用户名" size="large" v-model="user.username">
            <template #prefix>
              <el-icon>
                <User />
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input placeholder="登录密码" size="large" type="password" :show-password="true" v-model="user.password">
            <template #prefix>
              <el-icon>
                <Lock />
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-radio-group v-model="user.usertype">
            <el-radio-button label="学生" value="3" />
            <el-radio-button label="教师" value="2" />
            <el-radio-button label="管理员" value="1" />
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="large" @click="doLogin()">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<style scoped>
.box {
  height: 100%;
  width: 100%;
  background-image: url('@/assets/img/login-background.jpg');
  background-size: 100% 100%;
  background-position: center;
  background-repeat: no-repeat;
  display: flex;
  justify-content: center;
  align-items: center;
}

.el-card {
  width: 380px;
}

/* .el-card :deep(.el-card__body) {
  color: red;
} */
.el-radio-group {
  width: 100%;
}

.el-radio-group :deep(.el-radio-button) {
  flex-grow: 1;
}

.el-radio-group :deep(.el-radio-button .el-radio-button__inner) {
  width: 100%;
}

.el-button {
  width: 100%;
}
</style>
